package com.tcd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tcd.helper.redis.RedisHelper;
import com.tcd.helper.redis.ZSetRedisHelper;
import com.tcd.mapper.UserMapper;
import com.tcd.pojo.User;
import com.tcd.service.UserService;
import com.tcd.utils.AccountUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author space
 * @since 2021-05-21
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired(required = false)
    UserMapper userMapper;
    private static final String REDIS_VAL_NAME = "userSet";
    private static final String REDIS_VAL_KEY = "uid";

    @Override
    public User getUserByAccountAndPassword(String account, String password) {
        QueryWrapper<User> queryWrapper = new QueryWrapper();
        queryWrapper.eq("account", account);
        queryWrapper.eq("password", password);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public List<User> getUserFilter(Integer pageIndex, Integer pageSize, String userName, Integer roleId) {
        if (!RedisHelper.hasKey(REDIS_VAL_NAME)){
            List<User> list = userMapper.selectUserFilter(1, 0, null, null);
            ZSetRedisHelper.addSet(REDIS_VAL_NAME, REDIS_VAL_KEY, list, true);
        }
        if (AccountUtils.isNull(userName) && AccountUtils.isAllNull(roleId)) {
            return ZSetRedisHelper.getValSet(REDIS_VAL_NAME, User.class, (pageIndex - 1) * pageSize, pageSize);
        } else {
            return userMapper.selectUserFilter(pageIndex, pageSize, userName, roleId);
        }
    }

    @Override
    public Integer getUserFilterCount(String userName, Integer roleId) {
        if (AccountUtils.isNull(userName) && AccountUtils.isAllNull(roleId)) {
            return ZSetRedisHelper.getValCountSet(REDIS_VAL_NAME);
        } else {
            return userMapper.selectUserFilterCount(userName, roleId);
        }
    }

    @Override
    public Integer addUser(User user) {
        int row = userMapper.insert(user);
        if (row > 0) {
            ZSetRedisHelper.addSet(REDIS_VAL_NAME, REDIS_VAL_KEY, user);
        }
        return row;
    }

    @Override
    public Integer updateUser(User user) {
        int row = userMapper.updateById(user);
        if (row > 0) {
            ZSetRedisHelper.updateSet(REDIS_VAL_NAME, REDIS_VAL_KEY, userMapper.selectUserById(user.getUid()));
        }
        return row;
    }

    @Override
    public Integer deleteUsers(Integer[] ids) {
        int row = userMapper.deleteBatchIds(Arrays.asList(ids));
        if (row > 0) {
            ZSetRedisHelper.deleteSet(REDIS_VAL_NAME, ids);
        }
        return row;
    }

    @Override
    public Integer setUserRoles(Integer uid, Integer[] roleIds) {
        int row = userMapper.delUserRole(uid);
        if (roleIds.length > 0) {
            row += userMapper.setUserRoles(uid, roleIds);
        }
        if (row > 0) {
            ZSetRedisHelper.updateSet(REDIS_VAL_NAME, REDIS_VAL_KEY, userMapper.selectUserById(uid));
        }
        return row;
    }

    @Override
    public List<User> getUserByGroup(Integer groupId) {
        return userMapper.selectUserByGroup(groupId);
    }


}
